﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Data;

namespace CTIClassLibrary
{
    public class Common
    {
        //下面2个写法都ok
        //public static NBearLite.Database DB = new NBearLite.Database("CTIPay");
        public static NBearLite.Database DB = new NBearLite.Database(NBearLite.DatabaseType.SqlServer9, XMLConfig.Operation.GetSettings().ConnectionString);
        /// <summary>
        /// b2c 系统的 WebService 地址
        /// </summary>
        public static string WebServiceUrlB2c = XMLConfig.Operation.GetSettings().WebServiceUrlB2c;
        /// <summary>
        /// 中信银行的 WebService 支付网关地址
        /// </summary>
        public static string WebServiceUrlCitic = XMLConfig.Operation.GetSettings().WebServiceUrlCitic;

        /// <summary>
        /// 当天是否已结算
        /// </summary>
        public static bool hasSettled = false;

        /// <summary>
        /// 自动结算的时间
        /// </summary>
        public static int SettlementTime = 5;

        /// <summary>
        /// 中信银行付款批号
        /// </summary>
        public static string BatchNo_CITIC_Pay
        {
            get
            {
                object obj = Common.DB.Select(Tables.t_TraceNo, Tables.t_TraceNo.BatchNo)
                    .Where(Tables.t_TraceNo.BankName == "中信银行")
                    .ToScalar();

                if(obj!=null)
                {
                    return FormatCiticBatchNo(obj);
                }
                else
                    return "xxxxxx";
            }
            set
            {
                DataSet ds = Common.DB.Select(Tables.t_TraceNo, Tables.t_TraceNo.BatchNo, Tables.t_TraceNo.RefundBatchNo)
                   .Where(Tables.t_TraceNo.BankName == "中信银行")
                   .ToDataSet();

                //if (ds.Tables[0].Rows.Count >0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    int batchNo = Convert.ToInt32(dr[0]);
                    int batchNoRefund = Convert.ToInt32(dr[1]);
                    if (batchNo < batchNoRefund)
                    {
                        Common.DB.Update(Tables.t_TraceNo)
                            .AddColumn(Tables.t_TraceNo.BatchNo, Tables.t_TraceNo.RefundBatchNo + 1)
                            .Where(Tables.t_TraceNo.BankName == "中信银行")
                            .Execute();
                    }
                    else
                    {
                        Common.DB.Update(Tables.t_TraceNo)
                            .AddColumn(Tables.t_TraceNo.BatchNo, Tables.t_TraceNo.BatchNo + 1)
                            .Where(Tables.t_TraceNo.BankName == "中信银行")
                            .Execute();
                    }
                }
            }
        }

        /// <summary>
        /// 中信银行退款批号
        /// </summary>
        public static string BatchNo_CITIC_Refund
        {
            get
            {
                object obj = Common.DB.Select(Tables.t_TraceNo, Tables.t_TraceNo.RefundBatchNo)
                    .Where(Tables.t_TraceNo.BankName == "中信银行")
                    .ToScalar();

                if (obj != null)
                {
                    return FormatCiticBatchNo(obj);
                }
                else
                    return "xxxxxx";
            }
            set
            {
                DataSet ds = Common.DB.Select(Tables.t_TraceNo, Tables.t_TraceNo.BatchNo, Tables.t_TraceNo.RefundBatchNo)
                   .Where(Tables.t_TraceNo.BankName == "中信银行")
                   .ToDataSet();

                //if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    int batchNo = Convert.ToInt32(dr[0]);
                    int batchNoRefund = Convert.ToInt32(dr[1]);
                    if (batchNo < batchNoRefund)
                    {
                        Common.DB.Update(Tables.t_TraceNo)
                            .AddColumn(Tables.t_TraceNo.RefundBatchNo, Tables.t_TraceNo.RefundBatchNo + 1)
                            .Where(Tables.t_TraceNo.BankName == "中信银行")
                            .Execute();
                    }
                    else
                    {
                        Common.DB.Update(Tables.t_TraceNo)
                            .AddColumn(Tables.t_TraceNo.RefundBatchNo, Tables.t_TraceNo.BatchNo + 1)
                            .Where(Tables.t_TraceNo.BankName == "中信银行")
                            .Execute();
                    }
                }
            }
        }

        /// <summary>
        /// 中信银行交易流水号 1-999999 循环使用
        /// </summary>
        public static string TraceNo_CITIC
        {
            get
            {
                object obj = Common.DB.Select(Tables.t_TraceNo, Tables.t_TraceNo.TraceNo)
                    .Where(Tables.t_TraceNo.BankName == "中信银行")
                    .ToScalar();

                if (obj != null)
                {
                    int traceNo = Convert.ToInt32(obj);

                    if (traceNo > 999999)//循环使用
                        traceNo = 1;

                    DB.Update(Tables.t_TraceNo)
                        .AddColumn(Tables.t_TraceNo.TraceNo, traceNo + 1)
                        .Where(Tables.t_TraceNo.BankName == "中信银行")
                        .Execute();

                    return FormatCiticBatchNo(traceNo);
                }
                else
                    return "xxxxxx";
            }
        }

        /// <summary>
        /// 格式化中信银行批次号
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string FormatCiticBatchNo(object obj)
        {
            return obj.ToString().PadLeft(6, '0');//不足6位左补零
        }

        /// <summary>
        /// 取 XML 节点的值,大小写敏感
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public static string GetXMLElementValue(string xml, string element)
        {
            int start = xml.IndexOf("<" + element + ">");
            int end = xml.IndexOf("</" + element + ">");
            start += element.Length + 1;
            string retStr = xml.Substring(start + 1, end - start - 1);
            return retStr.Trim();
        }

        /// <summary>
        /// 将金额格式化成中信银行格式 以分为单位，不足13位左补零
        /// </summary>
        /// <param name="amountString"></param>
        /// <returns></returns>
        public static string CITICAmountFormat(decimal amount)
        {
            amount = Math.Abs(amount) * 100;//元 -> 分
            string amountString = string.Format("{0:0}", amount).PadLeft(13, '0');
            return amountString;
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="content"></param>
        /// <param name="logType">支付、退款、撤销、结算、对账</param>
        public static void LogIt(string content, string logType)
        {
            Common.DB.Insert(Tables.t_CITIC_Log)
                    .AddColumn(Tables.t_CITIC_Log.LogContent, content)
                    .AddColumn(Tables.t_CITIC_Log.LogTime, DateTime.Now)
                    .AddColumn(Tables.t_CITIC_Log.LogType, logType)
                    .Execute();
        }
    }
}
